﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace AleScheduler
{
    public partial class frmTriggers : Form
    {
        public frmTriggers()
        {
            InitializeComponent();
        }

        private string _schname;
        public string schName
        {
            get
            {
                return _schname;
            }
            set
            {
                _schname = value;
            }
        }

        private int _codeTrigger;
        private int codeTrigger
        {
            get { return _codeTrigger; }
            set { _codeTrigger = value; }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            trackBar1.Value = 0;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            trackBar1.Value = 1;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            trackBar1.Value = 2;
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            trackBar1.Value = 3;
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            trackBar1.Value = 4;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void frmTriggers_Load(object sender, EventArgs e)
        {
            UpdateListBox();
        }
        private void File_Drive()
        {
            if (radioButtonFile.Checked)
            {
                lblFile.Text = "File : ";
                cmbTestFile.Items.Clear();
                cmbTestFile.Items.Add("Open File");
                cmbTestFile.Items.Add("File Exist");
                cmbTestFile.Items.Add("File NOT Exist");
                buttonOpenFile.Visible = true;
                cmbDriveLetter.Visible = false;
                cmbTestFile.SelectedIndex = -1;
                txtFilename.Text = string.Empty;
                cmbDriveLetter.SelectedIndex = -1;
                cmbDriveLetter.Text = string.Empty;
            }
            else if (radioButtonDirectory.Checked)
            {
                lblFile.Text = "Dir : ";
                cmbTestFile.Items.Clear();
                cmbTestFile.Items.Add("Directory Exist");
                cmbTestFile.Items.Add("Directory NOT Exist");
                buttonOpenFile.Visible = true;
                cmbDriveLetter.Visible = false;
                cmbTestFile.SelectedIndex = -1;
                txtFilename.Text = string.Empty;
                cmbDriveLetter.SelectedIndex = -1;
                cmbDriveLetter.Text = string.Empty;
            }
            else
            {
                lblFile.Text = "Drive : ";
                cmbTestFile.Items.Clear();
                cmbTestFile.Items.Add("Drive Exist");
                cmbTestFile.Items.Add("Space Availaible");
                buttonOpenFile.Visible = false;
                cmbDriveLetter.Visible = true;
                //Fill ComboBox Drive Letter
                cmbDriveLetter.Items.Clear();
                System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
                foreach (System.IO.DriveInfo drive in drives)
                {
                    cmbDriveLetter.Items.Add(drive.Name.Substring(0, 2));
                }
                cmbTestFile.SelectedIndex = -1;
                txtFilename.Text = string.Empty;
                cmbDriveLetter.SelectedIndex = -1;
            }
            this.ToolTIP_HDSection();
        }

        private void CPU_RAM()
        {
            if (radioButtonCPU.Checked)
            {
                lblCPUuse.Text = "CPU use : ";
                maskedTextBoxCPU.Mask = "999";
                lblperc.Text = "%";
            }
            else
            {
                lblCPUuse.Text = "RAM use : ";
                maskedTextBoxCPU.Mask = "9999";
                lblperc.Text = "Mb";
            }
        }
        private void UpdateListBox()
        {
            listView1.Clear();
            listView1.Columns.Add("Number");
            listView1.Columns[0].Width = 55;
            listView1.Columns.Add("Trigger Type");
            listView1.Columns[1].Width = 100;
            listView1.Columns.Add("Kind Of Trigger");
            listView1.Columns[2].Width = 170;
            RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software", false).OpenSubKey("AleScheduler", false).OpenSubKey(this.schName, false);
            RegistryKey subKey;
            ListViewItem lvItem;
            string[] subKeys = reg.GetSubKeyNames();
            foreach (string strName in subKeys)
            {
                subKey = reg.OpenSubKey(strName);
                if (subKey == null)
                    continue;

                Int16 trgType = Convert.ToInt16(subKey.GetValue("KindofTrigger",0).ToString());
                string strType = "Nothing";
                string grpType = "Nothing";

                switch (trgType)
                {
                    case 101:
                        grpType = "Hard Disk Test";
                        strType = "Open File";
                        break;
                    case 102:
                        grpType = "Hard Disk Test";
                        strType = "File Exist";
                        break;
                    case 103:
                        grpType = "Hard Disk Test";
                        strType = "File NOT Exist";
                            break;
                    case 111:
                            grpType = "Hard Disk Test";
                            strType = "Directory Exist";
                            break;
                    case 112:
                            grpType = "Hard Disk Test";
                            strType = "Directory NOT Exist";
                            break;
                    case 121:
                            grpType = "Hard Disk Test";
                            strType = "Drive Exist";
                            break;
                    case 122:
                            grpType = "Hard Disk Test";
                            strType = "Space Availaible";
                            break;
                    case 201:
                            grpType = "CPU/RAM Test";
                            strType = "Fewer CPU Usage";
                            break;
                    case 202:
                            grpType = "CPU/RAM Test";
                            strType = "More CPU Usage";
                            break;
                    case 211:
                            grpType = "CPU/RAM Test";
                            strType = "Fewer RAM Usage";
                            break;
                    case 212:
                            grpType = "CPU/RAM Test";
                            strType = "More RAM Usage";
                            break;
                    case 301:
                            grpType = "Network Test";
                            strType = "Internet Connection";
                            break;
                    case 302:
                            grpType = "Network Test";
                            strType = "Ethernet Connection";
                            break;
                    case 303:
                            grpType = "Network Test";
                            strType = "Ping Success";
                            break;
                    case 401:
                            grpType = "User Test";
                            strType = "Logged User >";
                            break;
                    case 402:
                            grpType = "User Test";
                            strType = "Logged User <";
                            break;
                    case 403:
                            grpType = "User Test";
                            strType = "Current Username";
                            break;
                    case 501:
                            grpType = "System Test";
                            strType = "Idle Time";
                            break;
                    case 502:
                            grpType = "System Test";
                            strType = "UpTime";
                            break;
                    case 503:
                            grpType = "System Test";
                            strType = "Registry Key Exist";
                            break;
                    case 504:
                            grpType = "System Test";
                            strType = "Registry Key NOT Exist";
                            break;
                    case 505:
                            grpType = "System Test";
                            strType = "Service Exist";
                            break;
                    case 506:
                            grpType = "System Test";
                            strType = "Service NOT Exist";
                            break;
                    case 507:
                            grpType = "System Test";
                            strType = "Process Exist";
                            break;
                    case 508:
                            grpType = "System Test";
                            strType = "Process NOT Exist";
                            break;
                    case 601:
                            grpType = "Calendar Test";
                            strType = "Today is equal to";
                            break;
                    case 602:
                            grpType = "Calendar Test";
                            strType = "Today isn't equal to";
                            break;
                    case 603:
                            grpType = "Calendar Test";
                            strType = "Today is less than";
                            break;
                    case 604:
                            grpType = "Calendar Test";
                            strType = "Today is more than";
                            break;
                }

                lvItem = listView1.Items.Add(strName);
                lvItem.SubItems.Add(grpType);
                lvItem.SubItems.Add(strType);
            }
        }

        private void radioButtonDrive_CheckedChanged(object sender, EventArgs e)
        {
            File_Drive();
        }

        private void radioButtonFile_CheckedChanged(object sender, EventArgs e)
        {
            File_Drive();
        }

        private void cmbDriveLetter_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonOpenFile_Click(object sender, EventArgs e)
        {
            if (radioButtonDirectory.Checked)
            {
               FolderBrowserDialog dop = new FolderBrowserDialog();
                dop.Description = "AleScheduler - Select Directory To Test";
                if(dop.ShowDialog() == DialogResult.OK)
                    this.txtFilename.Text = dop.SelectedPath; 
            }
            else
            {
                OpenFileDialog op = new OpenFileDialog();
                op.CheckFileExists = true;
                op.Title = "AleScheduler - Select File To Test";
                if (op.ShowDialog() == DialogResult.OK)
                    this.txtFilename.Text = op.FileName;
            }
        }

        private void radioButtonDirectory_CheckedChanged(object sender, EventArgs e)
        {
            File_Drive();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true).OpenSubKey(this.schName,true);
            string CodeSub;
            if (this.btnAdd.Text == "Add")
            {
                CodeSub = (reg.SubKeyCount + 1).ToString();
                reg.CreateSubKey(CodeSub);
            }
            else
                CodeSub = Convert.ToString(this.codeTrigger);
            
            RegistryKey addreg = reg.OpenSubKey(CodeSub, true);
            switch (trackBar1.Value)
            {
                    //Case 0 - Hard Disk Triggers Save
                case 0:
                    //Check If Can Save Data
                    if (radioButtonFile.Checked || radioButtonDirectory.Checked)
                    {
                        if (cmbTestFile.SelectedIndex == -1 || txtFilename.Text == string.Empty)
                        {
                            MessageBox.Show("Insert Triggers Settings!", "AleScheduler - Triggers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }
                    else
                    {
                        if (cmbTestFile.SelectedIndex == -1 || cmbDriveLetter.Text == string.Empty)
                        {
                            MessageBox.Show("Insert Triggers Settings!", "AleScheduler - Triggers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }
                    //Save Data
                    if (radioButtonFile.Checked)
                    {
                        switch (cmbTestFile.SelectedIndex)
                        {
                            case 0:
                                addreg.SetValue("KindofTrigger", 101);
                                    break;
                            case 1:
                                    addreg.SetValue("KindofTrigger", 102);
                                    break;
                            case 2:
                                    addreg.SetValue("KindofTrigger", 103);
                                    break;
                        }
                        addreg.SetValue("Path", txtFilename.Text);
                    }
                    else if (radioButtonDirectory.Checked)
                    {
                        switch (cmbTestFile.SelectedIndex)
                        {
                            case 0:
                                addreg.SetValue("KindofTrigger", 111);
                                break;
                            case 1:
                                addreg.SetValue("KindofTrigger", 112);
                                break;
                        }
                        addreg.SetValue("Path", txtFilename.Text);
                    }
                    else
                    {
                        switch (cmbTestFile.SelectedIndex)
                        {
                            case 0:
                                addreg.SetValue("KindofTrigger", 121);
                                break;
                            case 1:
                                addreg.SetValue("KindofTrigger", 122);
                                break;
                        }
                        addreg.SetValue("Path", txtFilename.Text);
                        addreg.SetValue("Drive", cmbDriveLetter.Text);
                    }

                    //Bring Control to default state
                    cmbTestFile.SelectedIndex = -1;
                    txtFilename.Text = string.Empty;
                    cmbDriveLetter.SelectedIndex = -1;
                    cmbDriveLetter.Text = string.Empty;
                    break;

                    //Case 1 CPU-RAM Triggers Save
                case 1:
                    //Check If Can Save Data
                    if (radioButtonCPU.Checked || radioButtonRAM.Checked)
                    {
                        if (cmbCPURam.SelectedIndex == -1 || maskedTextBoxCPU.Text == string.Empty)
                        {
                            MessageBox.Show("Insert Triggers Settings!", "AleScheduler - Triggers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }
                    if (radioButtonCPU.Checked)
                    {
                        switch (cmbCPURam.SelectedIndex)
                        {
                            case 0:
                                addreg.SetValue("KindofTrigger", 201);
                                break;
                            case 1:
                                addreg.SetValue("KindofTrigger", 202);
                                break;
                        }     
                    }
                    else
                    {
                        switch (cmbCPURam.SelectedIndex)
                        {
                            case 0:
                                addreg.SetValue("KindofTrigger", 211);
                                break;
                            case 1:
                                addreg.SetValue("KindofTrigger", 212);
                                break;
                        }     
                    }
                    addreg.SetValue("Usage", maskedTextBoxCPU.Text);
                    break;
                    //Case 2 Network Test
                case 2:
                    if((this.cmbNetwork.SelectedIndex == -1) || (this.cmbNetwork.SelectedIndex == 2 && this.maskedTextBoxIP.Text == string.Empty))
                    {
                        MessageBox.Show("Insert Triggers Settings!", "AleScheduler - Triggers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    switch (this.cmbNetwork.SelectedIndex)
                    {
                        case 0:
                            addreg.SetValue("KindofTrigger", 301);
                            break;
                        case 1:
                            addreg.SetValue("KindofTrigger", 302);
                            break;
                        case 2:
                            addreg.SetValue("KindofTrigger", 303);
                            addreg.SetValue("IP", this.maskedTextBoxIP.Text);
                            break;
                    }
                    break;
                    //Case 3 Users Test
                case 3:
                    if ((this.cmUser.SelectedIndex == -1) || (this.txtUser.Text == string.Empty))
                    {
                        MessageBox.Show("Insert Triggers Settings!", "AleScheduler - Triggers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    switch (this.cmUser.SelectedIndex)
                    {
                        case 0:
                            addreg.SetValue("KindofTrigger", 401);
                            break;
                        case 1:
                            addreg.SetValue("KindofTrigger", 402);
                            break;
                        case 2:
                            addreg.SetValue("KindofTrigger", 403);
                            break;
                    }
                    addreg.SetValue("User", txtUser.Text);
                    break;
                    //Case 4 Generic system Test
                case 4:
                    if ((this.cmSystem.SelectedIndex == -1) || (this.txtSystem.Text == string.Empty))
                    {
                        MessageBox.Show("Insert Triggers Settings!", "AleScheduler - Triggers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    switch (this.cmSystem.SelectedIndex)
                    {
                        case 0:
                            addreg.SetValue("KindofTrigger", 501);
                            break;
                        case 1:
                            addreg.SetValue("KindofTrigger", 502);
                            break;
                        case 2:
                            addreg.SetValue("KindofTrigger", 503);
                            addreg.SetValue("KeyKind", this.cmbKindKey.SelectedIndex + 1);
                            break;
                        case 3:
                            addreg.SetValue("KindofTrigger", 504);
                            addreg.SetValue("KeyKind", this.cmbKindKey.SelectedIndex + 1);
                            break;
                        case 4:
                            addreg.SetValue("KindofTrigger", 505);
                            break;
                        case 5:
                            addreg.SetValue("KindofTrigger", 506);
                            break;
                        case 6:
                            addreg.SetValue("KindofTrigger", 507);
                            break;
                        case 7:
                            addreg.SetValue("KindofTrigger", 508);
                            break;
                    }
                    addreg.SetValue("FSystemTest", this.txtSystem.Text);
                    break;
                            //Case 5 Calendar Test
                        case 5:
                            if (this.cmbTodayIs.SelectedIndex == -1)
                            {
                                MessageBox.Show("Insert Triggers Settings!", "AleScheduler - Triggers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return;
                            }
                            switch (this.cmbTodayIs.SelectedIndex)
                            {
                                case 0:
                                    addreg.SetValue("KindofTrigger", 601);
                                    break;
                                case 1:
                                    addreg.SetValue("KindofTrigger", 602);
                                    break;
                                case 2:
                                    addreg.SetValue("KindofTrigger", 603);
                                    break;
                                case 3:
                                    addreg.SetValue("KindofTrigger", 604);
                                    break;
                            }
                            addreg.SetValue("DateTrigger", this.dateTimePicker1.Value.ToShortDateString());
                            addreg.SetValue("FromHourTrigger", this.textFromHour.Text.Trim());
                            addreg.SetValue("ToHourTrigger", this.textToHour.Text.Trim());
                            break;

            }
            this.btnAdd.Text = "Add";
            this.btnCancel1.Visible = false;
            UpdateListBox();
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string strCode = this.listView1.SelectedItems[0].Text;
                this.codeTrigger = Convert.ToInt16(strCode);
                RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software", false).OpenSubKey("AleScheduler", false).OpenSubKey(this.schName, false).OpenSubKey(strCode,false);
                int Code = Convert.ToInt32(reg.GetValue("KindofTrigger"));

                switch (Code)
                {
                        //Begin of Hard Disk Test Group
                    case 101:
                        this.trackBar1.Value = 0;
                        this.cmbTestFile.SelectedIndex = 0;
                        this.txtFilename.Text = (string)reg.GetValue("Path");
                        this.trackBar1.Value = 0;
                        this.radioButtonDirectory.Checked = false;
                        this.radioButtonDrive.Checked = false;
                        this.radioButtonFile.Checked = true;
                        break;
                    case 102:
                        this.trackBar1.Value = 0;
                        this.cmbTestFile.SelectedIndex = 1;
                        this.txtFilename.Text = (string)reg.GetValue("Path");
                        this.trackBar1.Value = 0;
                        this.radioButtonDirectory.Checked = false;
                        this.radioButtonDrive.Checked = false;
                        this.radioButtonFile.Checked = true;
                        break;
                    case 103:
                        this.trackBar1.Value = 0;
                        this.cmbTestFile.SelectedIndex = 2;
                        this.txtFilename.Text = (string)reg.GetValue("Path",string.Empty);
                        this.trackBar1.Value = 0;
                        this.radioButtonDirectory.Checked = false;
                        this.radioButtonDrive.Checked = false;
                        this.radioButtonFile.Checked = true;
                        break;
                    case 111:
                        this.trackBar1.Value = 0;
                        this.cmbTestFile.SelectedIndex = 0;
                        this.txtFilename.Text = (string)reg.GetValue("Path");
                        this.trackBar1.Value = 0;
                        this.radioButtonDirectory.Checked = true;
                        this.radioButtonDrive.Checked = false;
                        this.radioButtonFile.Checked = false;
                        break;
                    case 112:
                        this.trackBar1.Value = 0;
                        this.cmbTestFile.SelectedIndex = 1;
                        this.txtFilename.Text = (string)reg.GetValue("Path");
                        this.trackBar1.Value = 0;
                        this.radioButtonDirectory.Checked = true;
                        this.radioButtonDrive.Checked = false;
                        this.radioButtonFile.Checked = false;
                        break;
                    case 121:
                        this.trackBar1.Value = 0;
                        this.cmbTestFile.SelectedIndex = 0;
                        this.txtFilename.Text = (string)reg.GetValue("Path",string.Empty);
                        this.cmbDriveLetter.Text = (string)reg.GetValue("Drive", string.Empty);
                        this.trackBar1.Value = 0;
                        this.radioButtonDirectory.Checked = false;
                        this.radioButtonDrive.Checked = true;
                        this.radioButtonFile.Checked = false;
                        break;
                    case 122:
                        this.trackBar1.Value = 0;
                        this.cmbTestFile.SelectedIndex = 1;
                        this.txtFilename.Text = (string)reg.GetValue("Path", string.Empty);
                        this.cmbDriveLetter.Text = (string)reg.GetValue("Drive", string.Empty);
                        this.trackBar1.Value = 0;
                        this.radioButtonDirectory.Checked = false;
                        this.radioButtonDrive.Checked = true;
                        this.radioButtonFile.Checked = false;
                        break;

                        //Begin CPU-RAM Group Test
                    case 201:
                        this.trackBar1.Value = 1;
                        this.cmbCPURam.SelectedIndex = 0;
                        this.maskedTextBoxCPU.Text = (string)reg.GetValue("Usage", string.Empty);
                        this.radioButtonCPU.Checked = true;
                        this.radioButtonRAM.Checked = false;
                        break;
                    case 202:
                        this.trackBar1.Value = 1;
                        this.cmbCPURam.SelectedIndex = 1;
                        this.maskedTextBoxCPU.Text = (string)reg.GetValue("Usage", string.Empty);
                        this.radioButtonCPU.Checked = true;
                        this.radioButtonRAM.Checked = false;
                        break;
                    case 211:
                        this.trackBar1.Value = 1;
                        this.cmbCPURam.SelectedIndex = 0;
                        this.maskedTextBoxCPU.Text = (string)reg.GetValue("Usage", string.Empty);
                        this.radioButtonCPU.Checked = false;
                        this.radioButtonRAM.Checked = true;
                        break;
                    case 212:
                        this.trackBar1.Value = 1;
                        this.cmbCPURam.SelectedIndex = 1;
                        this.maskedTextBoxCPU.Text = (string)reg.GetValue("Usage", string.Empty);
                        this.radioButtonCPU.Checked = false;
                        this.radioButtonRAM.Checked = true;
                        break;

                        //Begin Network Group test
                    case 301:
                        this.trackBar1.Value = 2;
                        this.cmbNetwork.SelectedIndex = 0;
                        break;
                    case 302:
                        this.trackBar1.Value = 2;
                        this.cmbNetwork.SelectedIndex = 1;
                        break;
                    case 303:
                        this.trackBar1.Value = 2;
                        this.cmbNetwork.SelectedIndex = 2;
                        this.maskedTextBoxIP.Text = (string)reg.GetValue("IP", string.Empty);
                        break;

                        //Begin Users Test
                    case 401:
                        this.trackBar1.Value = 3;
                        this.cmUser.SelectedIndex = 0;
                        this.txtUser.Text = (string)reg.GetValue("User", string.Empty);
                        break;
                    case 402:
                        this.trackBar1.Value = 3;
                        this.cmUser.SelectedIndex = 1;
                        this.txtUser.Text = (string)reg.GetValue("User", string.Empty);
                        break;
                    case 403:
                        this.trackBar1.Value = 3;
                        this.cmUser.SelectedIndex = 2;
                        this.txtUser.Text = (string)reg.GetValue("User", string.Empty);
                        break;

                        //Begin System Generic
                    case 501:
                        this.trackBar1.Value = 4;
                        this.cmSystem.SelectedIndex = 0;
                        this.txtSystem.Text = (string)reg.GetValue("FSystemTest", string.Empty);
                        break;
                    case 502:
                        this.trackBar1.Value = 4;
                        this.cmSystem.SelectedIndex = 1;
                        this.txtSystem.Text = (string)reg.GetValue("FSystemTest", string.Empty);
                        break;
                    case 503:
                        this.trackBar1.Value = 4;
                        this.cmSystem.SelectedIndex = 2;
                        this.txtSystem.Text = (string)reg.GetValue("FSystemTest", string.Empty);
                        break;
                    case 504:
                        this.trackBar1.Value = 4;
                        this.cmSystem.SelectedIndex = 3;
                        this.txtSystem.Text = (string)reg.GetValue("FSystemTest", string.Empty);
                        break;
                    case 505:
                        this.trackBar1.Value = 4;
                        this.cmSystem.SelectedIndex = 4;
                        this.txtSystem.Text = (string)reg.GetValue("FSystemTest", string.Empty);
                        break;
                    case 506:
                        this.trackBar1.Value = 4;
                        this.cmSystem.SelectedIndex = 5;
                        this.txtSystem.Text = (string)reg.GetValue("FSystemTest", string.Empty);
                        break;
                    case 507:
                        this.trackBar1.Value = 4;
                        this.cmSystem.SelectedIndex = 6;
                        this.txtSystem.Text = (string)reg.GetValue("FSystemTest", string.Empty);
                        break;
                    case 508:
                        this.trackBar1.Value = 4;
                        this.cmSystem.SelectedIndex = 7;
                        this.txtSystem.Text = (string)reg.GetValue("FSystemTest", string.Empty);
                        break;
                        //Begin Time & Calendar Test
                    case 601:
                        this.trackBar1.Value = 5;
                        this.cmbTodayIs.SelectedIndex = 0;
                        this.dateTimePicker1.Value = Convert.ToDateTime((string)reg.GetValue("DateTrigger", string.Empty));
                        this.textFromHour.Text = (string)reg.GetValue("FromHourTrigger", string.Empty);
                        this.textToHour.Text = (string)reg.GetValue("ToHourTrigger", string.Empty);
                        break;
                    case 602:
                        this.trackBar1.Value = 5;
                        this.cmbTodayIs.SelectedIndex = 1;
                        this.dateTimePicker1.Value = Convert.ToDateTime((string)reg.GetValue("DateTrigger", string.Empty));
                        this.textFromHour.Text = (string)reg.GetValue("FromHourTrigger", string.Empty);
                        this.textToHour.Text = (string)reg.GetValue("ToHourTrigger", string.Empty);
                        break;
                    case 603:
                        this.trackBar1.Value = 5;
                        this.cmbTodayIs.SelectedIndex = 2;
                        this.dateTimePicker1.Value = Convert.ToDateTime((string)reg.GetValue("DateTrigger", string.Empty));
                        this.textFromHour.Text = (string)reg.GetValue("FromHourTrigger", string.Empty);
                        this.textToHour.Text = (string)reg.GetValue("ToHourTrigger", string.Empty);
                        break;
                    case 604:
                        this.trackBar1.Value = 5;
                        this.cmbTodayIs.SelectedIndex = 3;
                        this.dateTimePicker1.Value = Convert.ToDateTime((string)reg.GetValue("DateTrigger", string.Empty));
                        this.textFromHour.Text = (string)reg.GetValue("FromHourTrigger", string.Empty);
                        this.textToHour.Text = (string)reg.GetValue("ToHourTrigger", string.Empty);
                        break;
                }
                this.btnAdd.Text = "Save";
                this.btnCancel1.Visible = true;
            }
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            if ((e.Button == MouseButtons.Right && listView1.Items.Count > 0) && (listView1.SelectedItems.Count > 0) && (listView1.Items.Count == Convert.ToInt32(this.listView1.SelectedItems[0].Text)))
            {
                //Create a Context Menu
                ContextMenu mnuContextMenu = new ContextMenu();
                this.listView1.ContextMenu = mnuContextMenu;
                MenuItem mnuItemSetting = new MenuItem();
                mnuItemSetting.Text = "Delete";
                MenuItem mnuItemSetting3 = new MenuItem();
                mnuItemSetting3.Text = "Delete All";
                MenuItem mnuItemSetting2 = new MenuItem();
                mnuItemSetting2.Text = "Edit";
                mnuContextMenu.MenuItems.Add(mnuItemSetting2);
                mnuContextMenu.MenuItems.Add(mnuItemSetting);
                mnuContextMenu.MenuItems.Add(mnuItemSetting3);
                mnuItemSetting.Click += new EventHandler(DeleteTrigger);
                mnuItemSetting2.Click += new EventHandler(listView1_DoubleClick);
                mnuItemSetting3.Click += new EventHandler(DeleteAllTrigger);
            }
            else if ((e.Button == MouseButtons.Right && listView1.Items.Count > 0) && (listView1.SelectedItems.Count > 0))
            {
                ContextMenu mnuContextMenu = new ContextMenu();
                this.listView1.ContextMenu = mnuContextMenu;
                MenuItem mnuItemSetting2 = new MenuItem();
                mnuItemSetting2.Text = "Edit";
                MenuItem mnuItemSetting3 = new MenuItem();
                mnuItemSetting3.Text = "Delete All";
                mnuContextMenu.MenuItems.Add(mnuItemSetting3);
                mnuContextMenu.MenuItems.Add(mnuItemSetting2);
                mnuItemSetting3.Click += new EventHandler(DeleteAllTrigger);
                mnuItemSetting2.Click += new EventHandler(listView1_DoubleClick);
            }
        }

        private void DeleteAllTrigger(object sender, EventArgs e)
        {
            RegistryKey regKey = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true).OpenSubKey(this.schName, true);

            if (regKey == null)
            {
                MessageBox.Show(this, "Unable to open the registry key", "AleScheduler - Trigger");
                return;
            }
            string[] subKeys = regKey.GetSubKeyNames();
            foreach (string strnames in subKeys)
                regKey.DeleteSubKeyTree(strnames);

            regKey.Close();
            UpdateListBox();
        }
        private void DeleteTrigger(object sender, EventArgs e)
        {
            if (this.listView1.Items.Count > 0 && this.listView1.SelectedItems.Count > 0 && (listView1.Items.Count == Convert.ToInt32(this.listView1.SelectedItems[0].Text)))
            {
                ListView.SelectedListViewItemCollection lvItemCol = listView1.SelectedItems;
                System.Collections.IEnumerator colEnum = lvItemCol.GetEnumerator();
                colEnum.MoveNext();
                ListViewItem lvItem = (ListViewItem)colEnum.Current;
                RegistryKey regKey = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true).OpenSubKey(this.schName, true);

                if (regKey == null)
                {
                    MessageBox.Show(this, "Unable to open the registry key", "AleScheduler - Trigger");
                    return;
                }

                regKey.DeleteSubKeyTree(lvItem.Text);
                regKey.Close();
                UpdateListBox();
            }
        }

        private void listView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (this.listView1.Items.Count > 0 && this.listView1.SelectedItems.Count > 0 && (listView1.Items.Count == Convert.ToInt32(this.listView1.SelectedItems[0].Text)))
                {
                    ListView.SelectedListViewItemCollection lvItemCol = listView1.SelectedItems;
                    System.Collections.IEnumerator colEnum = lvItemCol.GetEnumerator();
                    colEnum.MoveNext();
                    ListViewItem lvItem = (ListViewItem)colEnum.Current;
                    RegistryKey regKey = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true).OpenSubKey(this.schName, true);

                    if (regKey == null)
                    {
                        MessageBox.Show(this, "Unable to open the registry key", "AleScheduler - Trigger");
                        return;
                    }

                    regKey.DeleteSubKeyTree(lvItem.Text);

                    UpdateListBox();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true).OpenSubKey(this.schName, true);
            reg.SetValue("TriggerTest", (this.listView1.Items.Count > 0) ? 1 : 0);
            reg.Close();
        }

        private void radioButtonCPU_CheckedChanged(object sender, EventArgs e)
        {
            CPU_RAM();
        }

        private void radioButtonRAM_CheckedChanged(object sender, EventArgs e)
        {
            CPU_RAM();
        }


        private void move_Slider()
        {
            switch (trackBar1.Value)
            {
                case 0:
                    grpDisk.Visible = true;
                    this.ToolTIP_HDSection();
                    grpCPURAM.Visible = false;
                    grpNetwork.Visible = false;
                    grpUser.Visible = false;
                    grpSystem.Visible = false;
                    groupBoxCalendarTest.Visible = false;
                    break;
                case 1:
                    grpDisk.Visible = false;
                    grpCPURAM.Visible = true;
                    grpNetwork.Visible = false;
                    grpUser.Visible = false;
                    grpSystem.Visible = false;
                    groupBoxCalendarTest.Visible = false;
                    break;
                case 2:
                    grpDisk.Visible = false;
                    grpCPURAM.Visible = false;
                    grpNetwork.Visible = true;
                    grpUser.Visible = false;
                    grpSystem.Visible = false;
                    groupBoxCalendarTest.Visible = false;
                    break;
                case 3:
                    grpDisk.Visible = false;
                    grpCPURAM.Visible = false;
                    grpNetwork.Visible = false;
                    grpUser.Visible = true;
                    grpSystem.Visible = false;
                    groupBoxCalendarTest.Visible = false;
                    break;
                case 4:
                    grpDisk.Visible = false;
                    grpCPURAM.Visible = false;
                    grpNetwork.Visible = false;
                    grpUser.Visible = false;
                    grpSystem.Visible = true;
                    groupBoxCalendarTest.Visible = false;
                    break;
                case 5:
                    grpDisk.Visible = false;
                    grpCPURAM.Visible = false;
                    grpNetwork.Visible = false;
                    grpUser.Visible = false;
                    grpSystem.Visible = false;
                    groupBoxCalendarTest.Visible = true;
                    break;
            }
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            move_Slider();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbNetwork.SelectedIndex == 2)
            {
                lblIPAdd.Visible = true;
                maskedTextBoxIP.Visible = true;
            }
            else
            {
                lblIPAdd.Visible = false;
                maskedTextBoxIP.Visible = false;
            }
        }

        public static bool validateNumericTextBox(char Key)
        {
            // Try converting the introduced char value to a byte value
            byte i;
            bool parsed = byte.TryParse(Key.ToString(), out i);
            bool val = true;

            //If convresion succeeded, show allow character.
            if (parsed)
            {
                val = false;
            }
            // If the conversion did not succeed, check if the pressed
            // button was Backspace (char code is 8) or Delete (char code is 46).
            else if (parsed == false)
            {
                switch (Convert.ToInt32(Key))
                {
                    default:
                        val = true;
                        break;
                    case 8:
                        val = false;  // If Backspace, allow char.
                        break;
                    case 46:
                        val = false;  // If Delete, allow char.
                        break;
                }
            }
            return val;
        }

        private void txtUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (cmUser.SelectedIndex != 2)
                e.Handled = validateNumericTextBox(e.KeyChar);
        }

        private void cmSystem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmSystem.SelectedIndex == 2 || cmSystem.SelectedIndex == 3)
            {
                cmbKindKey.Visible = true;
                cmSystem.Size = new Size(114, 21);
            }
            else
            {
                cmbKindKey.Visible = false;
                cmSystem.Size = new Size(175, 21);
            }
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            trackBar1.Value = 5;
        }

        private void frmTriggers_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }


        private void cmbTestFile_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ToolTIP_HDSection();
        }

        private void ToolTIP_HDSection()
        {
            if (radioButtonFile.Checked)
            {
                toolTipHD.SetToolTip(this.txtFilename, "Select File To Test");
            }
            else if (radioButtonDirectory.Checked)
            {
                toolTipHD.SetToolTip(this.txtFilename, "Select Directory To Test");
            }
            else
            {
                switch (this.cmbTestFile.SelectedIndex)
                {
                    case 0:
                        toolTipHD.SetToolTip(this.txtFilename, "Select Drive to Test");
                        break;
                    case 1:
                        toolTipHD.SetToolTip(this.txtFilename, "Insert amount of space avalaible on Hard Disk");
                        break;
                    default:
                        toolTipHD.SetToolTip(this.txtFilename, "Select Drive to Test");
                        break;
                }

            }
        }

        private void btnCancel1_Click(object sender, EventArgs e)
        {
            this.btnAdd.Text = "Add";
            UpdateListBox();
        }


    }
}
